package nc.ui.am.common;

public class TaskExecuter implements Runnable {
	private boolean isStopThread = false;
	private ITaskThreadPoolCallback callback = null;

	TaskExecuter(ITaskThreadPoolCallback callback) throws IllegalArgumentException {
		super();
		if (callback == null) {
			throw new IllegalArgumentException("Null callback passed");
		}
		
		this.callback = callback;
	}

	public void run() {
		while (!isStopThread) {
			Runnable task = null;
			synchronized (callback) {
				callback.incrementFreeThreadCount();
				while (!isStopThread) {
					//					_bStopExecution = false;
					task = callback.nextTask();
					if (task != null) {
						callback.decrementFreeThreadCount();
						break;
					} else {
						try {
							callback.wait();
						} catch (InterruptedException ignore) {
							// Ignore
						}
					}
				}
			}
			if (task != null) {
				try {
					task.run();
				} catch (Throwable th) {
					callback.showMessage(th);
				}
			}
		}
	}
}
